//

// (  )
//

package com.tools.acceleratecore.util;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import com.tools.acceleratecore.CleanCoreConstant;
import com.tools.acceleratecore.xlib.util.UtilEnv;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class UtilInstaller {

    public UtilInstaller() {
    }

    public static String getPackageDir(Context context) {
        return context == null ? null : context.getFilesDir().getParent();
    }

    public static String getPackageFileDir(Context context) {
        return context == null ? null : context.getFilesDir().getAbsolutePath();
    }

    public static String getPackageLibDir(Context context) {
        if (context == null) {
            return null;
        } else {
            String str = getPackageDir(context);
            return TextUtils.isEmpty(str) ? null : str + "/lib";
        }
    }

    public static String getPackageLibDirFile(Context context, String strFileName) {
        if (context != null && !TextUtils.isEmpty(strFileName)) {
            String str = getPackageLibDir(context);
            return TextUtils.isEmpty(str) ? null : str + "/" + strFileName;
        } else {
            return null;
        }
    }

    public static String getPackageOwnLibDir(Context context) {
        if (context == null) {
            return null;
        } else {
            String str = getPackageFileDir(context);
            return TextUtils.isEmpty(str) ? null : str + "/lib";
        }
    }

    public static String getPackageOwnLibDirFile(Context context, String strFileName) {
        if (context != null && !TextUtils.isEmpty(strFileName)) {
            String str = getPackageOwnLibDir(context);
            return TextUtils.isEmpty(str) ? null : str + "/" + strFileName;
        } else {
            return null;
        }
    }

    private static String getZipLibDir(Context context) {
        if (context == null) {
            return null;
        } else {
            String strCpuABI = UtilEnv.getCpuABI();
            String strZipLibPath = "lib/";
            if (TextUtils.isEmpty(strCpuABI)) {
                strZipLibPath = strZipLibPath + "armeabi";
            } else {
                strZipLibPath = strZipLibPath + strCpuABI;
            }

            return strZipLibPath;
        }
    }

    private static String getZipLibDirFile(Context context, String strFile) {
        if (context != null && !TextUtils.isEmpty(strFile)) {
            String str = getZipLibDir(context);
            return TextUtils.isEmpty(str) ? null : str + "/" + strFile;
        } else {
            return null;
        }
    }

    public static int loadLibrary(Context context) {
        if (!UtilEnv.getCpuABI().equals(UtilEnv.VALUE_STRING_CPU_ABI_ARMEABIV7A)) {
            return CleanCoreConstant.LOAD_LIB_FAILED;
        }

        if (context == null) {
            return CleanCoreConstant.LOAD_LIB_FAILED;
        } else {
            boolean bRet = false;
            try {
                System.loadLibrary(CleanCoreConstant.LIB_EFFICIENT_CORE_PROXY_NAME);
                return CleanCoreConstant.LOAD_LIB_SYSTEM;
            } catch (UnsatisfiedLinkError var3) {
                var3.printStackTrace();
                bRet = copyReloadSo(context);
                return bRet ? CleanCoreConstant.LOAD_LIB_OWN : CleanCoreConstant.LOAD_LIB_FAILED;
            }
        }
    }

    private static boolean copyReloadSo(Context context) {
        if (context == null) {
            return false;
        } else if (!copySo(context)) {
            return false;
        } else {
            return relaodSo(context);
        }
    }

    private static boolean copySo(Context context) {
        if (context == null) {
            return false;
        } else {
            try {
                Application application = (Application) context;
                ZipFile zipFile = new ZipFile(new File(application.getPackageResourcePath()));
                String[] strarraySo = new String[]{CleanCoreConstant.LIB_EFFICIENT_PROXY_INTACT_NAME, CleanCoreConstant.LIB_EFFICIENT_INTACT_NAME};
                File file = new File(getPackageOwnLibDir(context));
                file.mkdir();
                byte[] buffer = new byte[UtilEnv.VALUE_INT_BUFFER_SIZE];
                String[] var9 = strarraySo;
                int var8 = strarraySo.length;

                for (int var7 = 0; var7 < var8; ++var7) {
                    String strPath = var9[var7];
                    file = new File(getPackageOwnLibDirFile(context, strPath));
                    ZipEntry entry = zipFile.getEntry(getZipLibDirFile(context, strPath));
                    if (entry != null) {
                        if (file.exists()) {
                            if (file.length() == entry.getSize()) {
                                continue;
                            }

                            file.delete();
                        }

                        int nSize;
                        OutputStream outputStream = new FileOutputStream(file);
                        InputStream inputStream = zipFile.getInputStream(entry);

                        while ((nSize = inputStream.read(buffer)) > 0) {
                            outputStream.write(buffer, 0, nSize);
                        }

                        inputStream.close();
                        outputStream.close();
                        Runtime.getRuntime().exec("chmod 744 " + getPackageOwnLibDirFile(context, strPath));
                    }
                }

                return true;
            } catch (Exception var14) {
                var14.printStackTrace();
                return false;
            }
        }
    }

    private static boolean relaodSo(Context context) {
        if (context == null) {
            return false;
        } else {
            try {
                System.load(getPackageOwnLibDirFile(context, CleanCoreConstant.LIB_EFFICIENT_PROXY_INTACT_NAME));
                return true;
            } catch (UnsatisfiedLinkError var2) {
                var2.printStackTrace();
                return false;
            }
        }
    }
}
